package com.example.common.app;

import android.content.Context;

import com.example.common.factory.presenter.BaseContract;

/**
 * @author:0.2℃
 * @date： 2022-01-01 16:42
 * @version:
 * @Description ：
 */
public abstract class PresenterFragment<Presenter extends BaseContract.Presenter>
        extends Fragment
        implements BaseContract.View<Presenter> {

    protected Presenter mPresenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // 界面onAttach之后就要触发这个初始化presenter方法
        initPresenter();
    }

    /**
     * 在activity和fragment关联起来的时候触发
     * 初始化Presenter
     * @return 泛型的Prestenter
     */
    protected abstract Presenter initPresenter();

    @Override
    public void showError(int str) {
        // 显示错误，优先使用占位布局
        if (mPlaceholderView != null) {
            mPlaceholderView.triggerError(str);
        } else {
            Application.showToast(str);
        }
    }

    @Override
    public void showLoading() {
        //
        if (mPlaceholderView != null) {
            mPlaceholderView.triggerLoading();
        }
    }

    @Override
    public void setPresenter(Presenter presenter) {
        // 在VIew中赋值Presenter
        mPresenter = presenter;

    }
}
